
package com.shai.wool.admin.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.shai.wool.admin.entity.PostVO;
import com.shai.wool.admin.param.PostParam;

/**
 * Dao实现类
 *
 * @author 朱国军
 * @version 2016年04月09日 朱国军
 */
@Repository("adminDao")
public class AdminDao extends SqlSessionDaoSupport {
    
    @Resource(name = "sqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;

    @PostConstruct
    public void SqlSessionFactory() {
        super.setSqlSessionFactory(sqlSessionFactory);
    }
    
    /**
     * 按查询条件【分页】查询
     *
     * @param param 查询条件
     * @return 列表
     */
    public List<PostVO> queryByPage(PostParam param) {
        if (param.getPageNo() == null || param.getPageNo() == 0) {
            param.setPageNo(1);
        }
        if (param.getPageSize() == null || param.getPageSize() == 0) {
            param.setPageSize(10);
        }
        Integer dataCount = this.getSqlSession().selectOne("selectPostList_count", param);

        // 设置总记录数
        param.setDataCount(dataCount);
        // 设置总页码数
        Integer pageCount = dataCount % param.getPageSize() == 0 ? dataCount / param.getPageSize() : dataCount / param.getPageSize() + 1;
        param.setPageCount(pageCount);

        if (dataCount == 0) {
            param.setPageNo(1);
            return null;
        } else {
            Integer startNum = (param.getPageNo() - 1) * param.getPageSize();
            param.setStartNum(startNum);
            return this.getSqlSession().selectList("selectPostList", param);
        }
    }

    /**
     * 查询系统统计信息
     *
     * @return 获取系统统计信息
     */
    public Map<String, Object> queryStatData() {
        return this.getSqlSession().selectOne("query_stat_data");
    }
}
